import { yzPay } from "../../../utils/yz_pay"; //引入支付方法
import { payKeyboardAsync } from "utils/payKeyboard";
import payKeyboard from "components/payKeyboard";
export default {
  mixins: [yzPay],
  props: {
    overlayShow: {
      type: Boolean,
      default: false
    },
    newBlindBoxData: {
      type: null,
      default: {}
    },
    pageType: {
      type: String,
      default: ""
    }
  },
  components: { payKeyboard },
  data() {
    return {
      actionSheetShow: false,
      integralShow: false, //探币兑换弹出层
      postageShow: false, //邮费弹出层
      allData: {},
      postageNum: "",
      isDeliverGoods: true, //只点击一次发货，多次后不可以点击,
      popupSpecs: false,
      payarr: "",
      btnValue: {},
      payKeyboardShow: false
    };
  },
  mounted() {
    this.getOpenData();
  },
  methods: {
    async getOpenData() {
      let data = await $http.post(this.newBlindBoxData.api, { order_id: this.newBlindBoxData.order_id });
      if (data.result) {
        this.allData = data.data;
      } else {
        this.$toast(data.msg);
      }
    },
    close() {
      this.$emit("update:overlayShow", false);
    },
    // 点击弹出邮费
    clickWant() {
      if(!this.allData.canBuy) {
        return
      }
      if (this.allData.need_freight) {
        this.getpPstageData();
      } else {
        if (this.isDeliverGoods) {
          this.deliverGoods();
        }
      }
    },
    // 不需要运费就直接发货
    async deliverGoods() {
      let data = await $http.post("plugin.new-blind-box.frontend.activity.buy-blind-box", { order_id: this.newBlindBoxData.order_id });
      if (data.result) {
        this.$toast(data.msg);
        this.overlayShow = false;
        this.isDeliverGoods = false;
        this.$router.push(this.fun.getUrl('orderlist',{ status: '0', orderType: 'newBindBox' },{title: '我的盲盒'}));
      } else {
        this.$toast(data.msg);
      }
    },
    // 需要运费
    async getpPstageData() {
      let data = await $http.post("plugin.new-blind-box.frontend.goods-buy.index", { activity_order_id: this.newBlindBoxData.order_id });
      if (data.result) {
        for (let item of data.data.amount_items) {
          if (item.code == "total_goods_price") {
            this.postageNum = item.amount;
          }
        }
        this.postageShow = true;
      } else {
        this.$toast(data.msg);
      }
    },
    // 关闭邮费
    clickClosePostage() {
      this.postageShow = false;
    },
    // 兑换探币
    exchange() {
      this.integralShow = true;
    },
    // 取消兑换探币
    cancal() {
      this.integralShow = false;
    },
    // 邮费-支付
    async clickPay() {
      let data = await $http.post("plugin.new-blind-box.frontend.create.index", { activity_order_id: this.newBlindBoxData.order_id });
      if (data.result) {
        this.getPayData(data.data.order_ids);
      } else {
        this.$toast(data.msg);
      }
    },
    getPayData(order_ids) {
      $http
        .get("order.merge-pay", { order_ids })
        .then(response => {
          if (response.result == 1) {
            this.btnData = response.data.buttons;
            this.actionSheetShow = true;
            this.order_pay_id = response.data.order_pay.id;
          }
        })
        .catch(error => {
          console.log(error);
        });
    },
    //支付
    async payPost(value) {
      if (value.need_password || value.other?.need_password) {
        let pass = await this.getPayKeyboardPassword();
        this.getVerifyPassword(pass);
      } else {
        this.confirm(value);
      }
    },
    // 密码校验
    getVerifyPassword(pwd) {
      $http.get("payment.password.check", { password: pwd }, "").then(response => {
        if (response.result == 1) {
          //验证成功
          this.balancePay(pwd);
        } else {
          this.$dialog
            .confirm({ message: response.msg })
            .then(() => {
              if (response.data.code == 2002) {
                // 用户未设置支付密码
                // 去设置密码
                this.$router.push(this.fun.getUrl("set_balance_password", {}));
              }
            })
            .catch(() => {});
        }
      });
    },
    // 取消支付
    rewardPayCancelBtn() {
      this.actionSheetShow = false;
    },
    // 兑换 探币
    async exchangeIntegral() {
      let data = await $http.post("plugin.new-blind-box.frontend.activity.exchange", { order_id: this.newBlindBoxData.order_id });
      if (data.result) {
        this.$toast(data.msg);
        this.integralShow = false;
        this.overlayShow = false;
        if(this.pageType == "blind_box") {
          this.$router.push(this.fun.getUrl('orderlist',{ status: '0', orderType: 'newBindBox' },{title: '我的盲盒'}));
        }else {
          window.location.reload()
        }
      } else {
        this.$toast(data.msg);
      }
    },
    getPayKeyboardPassword() {
      this.payKeyboardShow = true;
      return new Promise((resove, reject) => {
        payKeyboardAsync.addCompleteFn(pass => {
          resove(pass);
        });
      });
    },
    payComplete(text) {
      payKeyboardAsync.complete(text);
    }
  }
};
